/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Coordinator;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * The class that controls the drawing and setting up the Menu that allows the user to
 * choose between the GUI and text based versions of the ame.
 * @author Luke
 */
public class Gui_Menu extends JFrame { //Setup the objects

    private JFrame frame = new JFrame("Traffic Sim GUI v2.0"); //Make the frame
    private JPanel canvas = new JPanel(); //Make the canvas
    private Font f = new Font("Serif", Font.PLAIN, 36);
    private Font f2 = new Font("Serif", Font.PLAIN, 14);
    private JPanel middle = new JPanel();
    private JPanel bottom = new JPanel();
    private JPanel top = new JPanel();
    private JLabel title = new JLabel("Traffic Simulator V2.0"); //set title of the window
    private JLabel instructions = new JLabel("Select which simulation you wish to run:"); //make new label for instrcutions

    /**
     *The constructor of the Gui Menu, that sets the screen size and visibility of the Menu.
     */
    public Gui_Menu() {
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE); //when the "X" is hit the program will end
        frame.setVisible(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //gets size of screen
        frame.setSize(screenSize.width, screenSize.height); //makes frame size full screen
        frame.setResizable(false); //the user cannot resize the window
        top.setName("top");
        middle.setName("middle");
        bottom.setName("bottom");

    }

    /**
     *The components for the gui menu are added into the JFrame
     */
    public void makeMenu() {

        frame.add(canvas); //add canvas to frame
        canvas.setVisible(true); //make canvas visible
        canvas.setLayout(new GridLayout(3, 1)); //make canvas have grid layout of 3 rows 1 column

        top.setVisible(true); //make top jpanel viewaBLE
        top.setLayout(new GridLayout(2, 1)); //give top jpanel a grid layout of 2 rows 1 column
        middle.setVisible(true); //make middle jpanel viewable
        middle.setLayout(new GridLayout(1, 0)); //give middle jpanel a grid layout of 1 row and x colunms
        bottom.setVisible(true); //mak middle jpanel viewable

        canvas.add(top); //add jpanel top to canvas
        canvas.add(middle); //add jpanl middle to canvas
        canvas.add(bottom); //add jpanel bottom to canvas

        title.setHorizontalAlignment(JLabel.CENTER); //centre the title of layout
        title.setFont(f); //set font of the title to defined font instance

        JLabel footer = new JLabel("Designed by Luke Bermingham"); //set the footer of window
        footer.setFont(f2); //set font type of footer

        instructions.setHorizontalAlignment(JLabel.CENTER); //centre the instructions
        instructions.setFont(f2); //set the font type for the instructions

        top.add(title); //add the title to the top jpanel
        top.add(instructions); //add the instructions to the top jpanel
        bottom.add(footer); //add the footer to the bottom jpanel

        makeButtons(); //Makes the buttons

    }

    /**
     *A method that creates the buttons for the Gui menu: Graphics and Text
     */
    public void makeButtons() {

        JButton normal = new JButton("Text"); //text button
        normal.setFont(f);


        JButton gui = new JButton("Graphics"); //graphics button
        gui.setFont(f);

        middle.add(gui); //add buttons to Gui menu
        middle.add(normal);

        TextHandler textHandler = new TextHandler(); //instanitiates handler
        GuiHandler guiHandler = new GuiHandler(); // as above
        normal.addActionListener(textHandler); //add handler to manage button clicks
        gui.addActionListener(guiHandler); // as above

    }

    private class TextHandler implements ActionListener { //When the text button is clicked

        public void actionPerformed(ActionEvent event) {
            frame.setVisible(false); //Set visiblity to invisble
            Coordinator coordinator = new Coordinator(); //instantiate the corrodinator class
            coordinator.main(null); //run the main method in the coordinator
        }
    }

    private class GuiHandler implements ActionListener { //When the Gui button is clicked

        public void actionPerformed(ActionEvent event) {
            instructions.setText("Please enter the simulation specifics:"); //Sets text for a JLabel
            middle.removeAll(); //Remove all elems in the middle JFrame
            frame.repaint(); //repaint the frame
            middle.validate(); //Re validates all components in the middle JPanel
            Gui_Options guiOptions = new Gui_Options(canvas, frame); //Instantiates the gui options class
        }
    }
}
